package com.own.business.level.controller;

import com.own.business.level.business.service.LevelUserService;
import com.own.business.level.entity.po.LevelUser;
import com.own.business.level.entity.vo.LevelUserVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * LevelUserController
 *
 * @author chenxueli
 * @date 2022-07-25 11:48:00
 */
@Api(tags = "用户等级" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping("level/user")
public class LevelUserController extends BaseController {

    @Resource
    public LevelUserService levelUserService;

    @ApiOperation(value = "根据用户id获取等级信息")
    @GetMapping("/userId/{userId}")
    public ResultModel<LevelUserVo> getByUserId(
            @ApiParam(value = "用户id", required = true) @PathVariable Long userId
    ) {
        var vo = levelUserService.getById(LevelUser::getFkUserId, userId, () -> levelUserService.buildByUserId(userId));
        return R.success(vo);
    }

    @ApiOperation(value = "获取当前登录用户的等级信息")
    @GetMapping("/userId")
    public ResultModel<LevelUserVo> getBySelf() {
        var loginUser = SessionUserUtil.loginUser();
        loginUser.checkLogin();
        return getByUserId(loginUser.userId());
    }

}
